﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using System;
using GameControl;
public class ShootEnemy :EnemyItem
{
   // public GameObject HandHold;
   [HideInInspector]
    public float HandHoldRelativeAngle = 0;
    [HideInInspector]
    public bool HandHoldRelativeFlip = true;
    private string handHoldName;

   /* public override ItemField UploadInstanceField()
    {
       ItemFieldSetting=  base.UploadInstanceField();
        Debug.Assert(HandHold!=null);
        GetField<ShootEnemyField>().Weapon = HandHold.name;
        return ItemFieldSetting;
    }
    public override void DownloadInstanceField(object args)
    {
        base.DownloadInstanceField(args);
        Debug.Assert(HandHold != null);
        HandHold.name = GetField<ShootEnemyField>().Weapon;
    }*/
    public override void OnStart()
    {
        base.OnStart();
        MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.CreateInMap, this, new GameMap.ItemCreateArgs(GetField<EnemyItemField>().DropItemName, ItemFieldSetting.Position, (ItemField x) =>
        {
            //      (x as AttackExtentItemField).Power = 0;
            x.Position = this.transform.position;
            x.Blood = -1;
            x.Mass = 0;
            handHoldName= x.ItemName;
      //      this.GetField<EnemyItemField>().DropItemName = GetField<EnemyItemField>().DropItemName;
            GameManager.Get<IItemInstantiateManager>().GetItemFieldByName(handholdExtentName).IsUpdate = false;
            GameManager.Get<IItemHealthManager>().IgnorePairs.UniqueAdd(new Tuple<string, string>(handHoldName, ItemFieldSetting.ItemName));
            GameManager.Get<IItemHealthManager>().IgnorePairs.UniqueAdd(new Tuple<string, string>(handHoldName,handholdExtentName ));
        }));
        handHoldvelocity = Vector3.zero;
    }
    public CombinableItem handHoldItem
    {
        get
        {
            var tp = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(handHoldName);
            if (tp != null) return tp.GetComponent<CombinableItem>();
            else return null;
        }
    }
    public override void RefreshFlip()
    {
        base.RefreshFlip();
        if (handHoldItem != null)
        {
            handHoldItem.RotationInDegree = HandHoldRelativeAngle;
            handHoldItem.transform.localScale = new Vector3(this.sprite.transform.localScale.x * (HandHoldRelativeFlip ? -1 : 1), 1,1 );
        }
    }
    private Vector3 handHoldvelocity = Vector3.zero;
    Vector3 ptr = default;
    public override void RefreshHandHoldCollider()
    {
        base.RefreshHandHoldCollider();
        Debug.Log("Item=" + GetField<EnemyItemField>().DropItemName);
        if (handHoldItem != null&&_handHold!=null)
        {
           

            handHoldItem.HaveDestoryEffects = false;

            var item = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(handholdExtentName);
            if (item == null)
            {
                return;
            }
            var bounds = UnsafeTool.GetBounds((item.GetComponent<AttackExtentItem>().ItemFieldSetting as AttackExtentItemField).TriggerColliderPoints.ConvertAll(s => (Vector3)s));
            var pos=item.transform.TransformPoint(bounds.center-new Vector3(0, bounds.extents.y,0));
            /* if (handHoldItem.transform.parent.GetComponent<Rigidbody2D>())
             {
                 handHoldItem.transform.parent.gameObject.layer = this.gameObject.layer;
             }*/
            ptr = pos;
            handHoldItem.transform.position = pos;
            if(handHoldItem.ItemFieldSetting is CombinableItemField)
            {
                var rigid = handHoldItem.GetComponentInParent<Rigidbody2D>();
                rigid.gravityScale = 0;
                rigid.gameObject.ChangeLayer(this.gameObject.layer);
            }
            else
            {
                handHoldItem.gameObject.ChangeLayer(this.gameObject.layer);

            }
        //    handHoldItem.transform.position = Vector3.SmoothDamp(handHoldItem.transform.position,pos,
       //     ref handHoldvelocity, 0.05f);
        }
    }
    public override void Attack()
    {
        base.Attack();
        if (handHoldItem != null)
        {
            var list = handHoldItem.GetComponents<ItemFunc>();
            foreach(var x in list)
            {
                x.InvokeByEnmey();
            }

        }
    }
    public override void OnPreferenceDestory()
    {
        base.OnPreferenceDestory();
        if (handHoldName != default)
        {
            //当自身销毁,连带销毁子物体
            MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.DeleteInMap, this, new GameMap.ItemMapArgs { ItemName = handHoldName }, null);
        }
        handHoldName = default;
    }
    private void OnDrawGizmos()
    {
        if (ptr != default)
        {
            Gizmos.DrawCube(ptr, Vector3.one * 0.1f);
        }
        ptr = default;
    }
}
